﻿using K9Nano.MultiTenancy;

namespace K9Nano.Authorization
{
    public sealed class ApiResource : IApiResource
    {
        public string Controller { get; }

        public string Action { get; }

        public string DisplayName { get; }

        public string Description { get; }

        public EMultiTenancySides MultiTenancySide { get; }

        public string PrimaryId { get; }

        public ApiResource(string controller, 
            string action,
            string displayName, 
            string description,
            EMultiTenancySides multiTenancySide)
        {
            Controller = controller;
            Action = action;
            DisplayName = displayName;
            Description = description;
            MultiTenancySide = multiTenancySide;
            PrimaryId = $"{controller}.{action}";
        }
        
        public override int GetHashCode()
        {
            return PrimaryId.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is ApiResource other)
            {
                return PrimaryId == other.PrimaryId;
            }

            return false;
        }

        public static bool operator ==(ApiResource left, ApiResource right)
        {
            if (Equals(left, null))
            {
                return Equals(right, null);
            }

            return left.Equals(right);
        }

        public static bool operator !=(ApiResource left, ApiResource right)
        {
            return !(left == right);
        }

        public override string ToString()
        {
            return PrimaryId;
        }
    }
}